DEFINE STREAM bf .
DEFINE VAR v_id LIKE code_cmmt .
DEFINE  VAR v_num AS INTE . 
DEFINE VAR fnamea AS CHAR .
DEFINE BUFFER cdmstr FOR code_mstr.
define variable voutput as character .
/* assign voutput = "d:\" .  */

 IF SEARCH(fnamea) <> ? THEN
    OS-DELETE VALUE(fnamea). 

 assign voutput = "\\192.168.12.12\shared$\output\" .  
fnamea = voutput + "vfdbak" + ".csv"  .
FIND FIRST cdmstr WHERE code_fldname = "vfdbak"
                   AND CODE_value = "vfd"  NO-LOCK NO-ERROR .
IF AVAIL cdmstr  THEN     DO:
    v_id = cdmstr.code_cmmt .
END.

/* declare  v_tot cursor  FOR                           */
/* SELECT COUNT(*) FROM vfd_det  WHERE vfd_id > v_id .  */
/* OPEN v_tot .                                         */
/* FETCH v_tot INTO v_num .                             */
/* CLOSE v_tot .                                        */

 
 
     output stream bf to value(fnamea) .
     EXPORT STREAM bf DELIMITER "," "vf_id" "vfd_group" "vfd_number" "vfd_part" "vfd_sub_part"  "vfd_type" "vfd_status"
      "vfd_pm_code" "vfd_site" "vfd_req_site" "vfd_buyer" "vfd_req_buyer" "vfd_vend" "vfd_price" "vfd_rel_date" "vfd_due_date"
      "vfd_qty_req" "vfd_qty_avail" "vfd_qty_stk" "vfd_qty_pod" "vfd_qty_demand" "vfd_all_pct" "vfd_all_pre" "vfd_all_eff"
      "vfd_qty_phantom" "vfd_sfty_stk" "vfd_rsn_code" "vfd_reason" "vfd_rmks" "vfd_curr" "vfd__chr01" "vfd__chr02" "vfd__chr03" "vfd__chr04" "vfd__dec01" "vfd__dec02" "vfd__dec03" "vfd__dec04" "vfd__int01"  "vfd__int02" "vfd__dte01" "vfd__dte02" "vfd__log01" "vfd__log02"
      .
        FOR EACH vfd_det NO-LOCK WHERE  vfd_id > v_id BREAK BY vfd_id :
               EXPORT STREAM bf DELIMITER "," vfd_id vfd_group vfd_number vfd_part vfd_sub_part
                      vfd_type vfd_status  vfd_pm_code vfd_site vfd_req_site vfd_buyer vfd_req_buyer vfd_vend  vfd_price vfd_rel_date
                      vfd_due_date  vfd_qty_req vfd_qty_avail vfd_qty_stk vfd_qty_pod vfd_qty_demand vfd_all_pct vfd_all_pre vfd_all_eff
                      vfd_qty_phantom vfd_sfty_stk  vfd_rsn_code vfd_reason vfd_rmks vfd_curr vfd__chr01 vfd__chr02 vfd__chr03 vfd__chr04 vfd__dec01 vfd__dec02 vfd__dec03 vfd__dec04 vfd__int01 vfd__int02
                      TODAY  TODAY   vfd__log01 vfd__log02 .
               IF LAST(vfd_id) THEN  DO:
                   FIND first CODE_mstr WHERE CODE_fldname = "vfdbak"
                                       AND CODE_value = "vfd"
                                       EXCLUSIVE-LOCK NO-ERROR .
                   IF NOT AVAIL CODE_mstr  THEN  DO:
                      CREATE CODE_mstr. 
                      ASSIGN  CODE_fldname = "vfdbak"
                              code_value   = "vfd"
                              code_cmmt    = vfd_id .

                  END.
                  ELSE DO:
                      CODE_cmmt = vfd_id .
                  END.
               END.
      END.
      OUTPUT STREAM bf CLOSE .
 
  QUIT . 
